package com.hoshiicloud.user.service.impl.rbac;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.rbac.entity.SysPermissiones;
import com.hoshiicloud.user.mapper.rbac.StoredProcedureMapper;
import com.hoshiicloud.user.mapper.rbac.SysPermissionesMapper;
import com.hoshiicloud.user.param.rbac.GetChildListParam;
import com.hoshiicloud.user.service.rbac.ISysPermissionesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-18
 */
@Service
public class SysPermissionesServiceImpl extends ServiceImpl<SysPermissionesMapper, SysPermissiones>
		implements ISysPermissionesService {

	@Autowired
	private SysPermissionesMapper sysPermissionesMapper;

	@Autowired
	private StoredProcedureMapper storedProcedureMapper;

	@Override
	@Cacheable(cacheNames = { "sysPermissiones" }, key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "权限相关-权限信息管理-获取子权限列表")
	public Response getChildList(String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("权限不存在");
		}
		// 设置表的信息
		String idColumnName = "id";
		String parentIdColumnName = "pid";
		String tableName = "sys_permissiones";
		GetChildListParam param = new GetChildListParam(id, idColumnName, parentIdColumnName, tableName);
		storedProcedureMapper.getChildListStr(param);
		// 从入参中取得返回值
		String childIdListStr = param.getRetVal();
		/*
		 * if (StringUtils.isBlank(childIdListStr)) { return null; }
		 */
		QueryWrapper<SysPermissiones> queryWrapper = new QueryWrapper<SysPermissiones>();
		queryWrapper.inSql("pid", childIdListStr);
		List<SysPermissiones> list = sysPermissionesMapper.selectList(queryWrapper);
		return Response.data(list);
	}

	@Override
	@CacheEvict(cacheNames = { "sysPermissiones" }, allEntries = true)
	@Logable(authName = "权限相关-权限信息管理-新增权限信息")
	public Response addPermission(SysPermissiones param) {
		int count = 0;
		if (param.getPId() != null) {
			count = sysPermissionesMapper.selectCount(new QueryWrapper<SysPermissiones>().eq("id", param.getPId()));
			if (count <= 0) {
				throw new ServiceException(CommonError.error("新增失败，上级权限不存在"));
			}
		}
		count = sysPermissionesMapper.insert(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("新增失败"));
		}
		return Response.success("新增成功");
	}

	@Override
	@CacheEvict(cacheNames = { "sysModel", "sysPermissiones" }, allEntries = true)
	@Logable(authName = "权限相关-权限信息管理-修改权限信息")
	public Response updatePermissionById(SysPermissiones param) {
		int count = sysPermissionesMapper.updateById(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("修改失败"));
		}
		return Response.success("修改成功");
	}

	@Override
	@CacheEvict(cacheNames = { "sysModel", "sysPermissiones" }, allEntries = true)
	@Logable(authName = "权限相关-权限信息管理-删除权限信息")
	public Response deletePermissionById(String id) {
		int count = sysPermissionesMapper.selectCount(new QueryWrapper<SysPermissiones>().eq("pid", id));
		if (count > 0) {
			throw new ServiceException(CommonError.error("删除失败,存在下级权限"));
		}
		count = sysPermissionesMapper.deleteById(id);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("删除失败"));
		}
		return Response.success("删除成功");
	}

	@Override
	@Cacheable(cacheNames = { "sysPermissiones" }, key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "权限相关-权限信息管理-查询权限信息详情")
	public Response getDetail(String id) {
		SysPermissiones sysPermissiones = sysPermissionesMapper.selectById(id);
		if (sysPermissiones == null) {
			return Response.failed("数据不存在");
		}
		return Response.data(sysPermissiones);

	}

	@Override
	@Cacheable(cacheNames = "sysPermissiones", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5('Wrapper(SqlSelect=' +#queryWrapper.getSqlSelect() + ', SqlSegment=' +#queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "权限相关-权限信息管理-查询权限信息列表")
	public Response getList(QueryWrapper<SysPermissiones> queryWrapper) {
		return Response.data(sysPermissionesMapper.selectList(queryWrapper));
	}

	@Override
	@Cacheable(cacheNames = "sysPermissiones", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5('Page(current=' +#page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' +#queryWrapper.getSqlSelect() + ', SqlSegment=' +#queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "权限相关-权限信息管理-分页查询权限信息")
	public Response<IPage<SysPermissiones>> getPage(IPage<SysPermissiones> page, QueryWrapper<SysPermissiones> queryWrapper) {
		return Response.data(sysPermissionesMapper.selectPage(page, queryWrapper));
	}


	@Override
	public List<SysPermissiones> getSysPermissionesByUserId(Long userId) {
		return sysPermissionesMapper.listSysPermissionesByUserId(userId);
	}


}
